summaryrefslogtreecommitdiffstats
path: root/src/main/java/org/uic/barcode/ssbFrame/SsbFrame.java
blob: 2c8f66f6702e20e0382c6dc8082f366ae7cab8c2 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
package org.uic.barcode.ssbFrame;

import java.io.IOException;
import java.math.BigInteger;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.PrivateKey;
import java.security.Provider;
import java.security.PublicKey;
import java.security.Security;
import java.security.Signature;
import java.security.SignatureException;
import java.security.Provider.Service;
import java.util.Arrays;

import org.uic.barcode.ticket.EncodingFormatException;
import org.uic.barcode.utils.AlgorithmNameResolver;
import org.uic.barcode.utils.SecurityUtils;

public class SsbFrame {
	
	private SsbHeader header = new SsbHeader();
	
	private byte[] signaturePart1 = null;
	
	private byte[] signaturePart2 = null;
	
	private SsbNonUic nonUicData = null;
	
	private SsbNonReservation nonReservationData = null;
	
	private SsbReservation reservationData = null;
	
	private SsbGroup groupData = null;
	
	private SsbPass passData = null;
	
	public void decode(byte[] bytes) throws EncodingFormatException {
		
		if (bytes.length != 114) {
			throw new EncodingFormatException("Data size does not fit to SSB");
		}
		
		if (header == null) {
			header = new SsbHeader();
		}
		
		int offset = 0;
		
		offset = offset + header.decodeContent(bytes,0);
		
		if (header.getTicketType().equals(SsbTicketType.UIC_1_IRT_RES_BOA)) {
			
			reservationData = new SsbReservation();
			offset = offset + reservationData.decodeContent(bytes,offset);	
			
		} else if (header.getTicketType().equals(SsbTicketType.UIC_2_NRT)) {
			
			nonReservationData = new SsbNonReservation();
			offset = offset + nonReservationData.decodeContent(bytes,offset);
			
		} else if (header.getTicketType().equals(SsbTicketType.UIC_3_GRP)) {
			
			groupData = new SsbGroup();
			offset = offset + groupData.decodeContent(bytes, offset);

		} else if (header.getTicketType().equals(SsbTicketType.UIC_4_RPT)) {

			passData = new SsbPass();
			offset = offset + passData.decodeContent(bytes,offset);
			
		} else {
			 
			nonUicData = new SsbNonUic();
			offset = offset + nonUicData.decodeContent(bytes,offset);
			
		}
		
		byte[] signatureBytes = new byte[56];
				
		try {
			//check for non-standard signature encoding
			BigInteger[] bInts = SecurityUtils.decodeSignatureIntegerSequence(signatureBytes);
			SecurityUtils.encodeSignatureIntegerSequence(bInts[0],bInts[1]);
			signaturePart1 = bInts[0].toByteArray();
			signaturePart2 = bInts[1].toByteArray();	
			//decoding the entire signature was ok, so there was no split
		} catch (Exception e) {
			//the signature is correctly implemented, continue with recombination
			signaturePart1 = new byte[28];
			signaturePart2 = new byte[28];	
			
			for (int i = 0 ; i < 28;i++) {
				signaturePart1[i] = bytes[58 + i];
				signaturePart2[i] = bytes[58 + 28 + i];
			}
		}

		
	}
	


	
	public byte[] encode() throws EncodingFormatException {
		
		byte[] bytes = new byte[114];
		
		int offset = header.encodeContent(bytes,0);
		
		
		
		if (nonUicData != null) {
			offset = nonUicData.encodeContent(bytes, offset);
		} else if (nonReservationData != null) {
			offset = nonReservationData.encodeContent(bytes, offset);
		} else if (reservationData != null) {
			offset = reservationData.encodeContent(bytes, offset);
		} else if (groupData != null) {
			offset = groupData.encodeContent(bytes, offset);
		} else if (passData != null) {
			offset = passData.encodeContent(bytes, offset);
		} else {
			throw new EncodingFormatException("Data Content for SSB missing");
		};
		
		
		if (signaturePart1.length > 28) {
			throw new EncodingFormatException("Signature too large");
		}
		if (signaturePart2.length > 28) {
			throw new EncodingFormatException("Signature too large");
		}
		
		for (int i = 1 ; i < 29; i++) {
			int sigInd = signaturePart1.length - i;
			if (sigInd < signaturePart1.length && sigInd >= 0) {
				bytes[58 + 28 - i] = signaturePart1[sigInd];
			} else {
				bytes[58 + 28 - i] = '\0';
			}
			sigInd = signaturePart2.length - i;
			if (sigInd < signaturePart2.length && sigInd >= 0) {
				bytes[58 + 28 + 28 - i] = signaturePart2[sigInd];
			} else {
				bytes[58 + 28 + 28 - i] = '\0';
			}
		}
		
				
		return bytes;
		
	}
	
	public byte[] getDataForSignature() throws EncodingFormatException {
		
		byte[] bytes = new byte[58];
		
		int offset = header.encodeContent(bytes,0);
		
				
		if (nonUicData != null) {
			offset = nonUicData.encodeContent(bytes, offset);
		} else if (nonReservationData != null) {
			offset = nonReservationData.encodeContent(bytes, offset);
		} else if (reservationData != null) {
			offset = reservationData.encodeContent(bytes, offset);
		} else if (groupData != null) {
			offset = groupData.encodeContent(bytes, offset);
		} else if (passData != null) {
			offset = passData.encodeContent(bytes, offset);
		} else {
			throw new EncodingFormatException("Data Content for SSB missing");
		};
				
		return bytes;
		
	}

	public SsbHeader getHeader() {
		return header;
	}

	public void setHeader(SsbHeader header) {
		this.header = header;
	}

	public byte[] getSignaturePart1() {
		return signaturePart1;
	}

	public void setSignaturePart1(byte[] signaturePart1) {
		this.signaturePart1 = signaturePart1;
	}

	public byte[] getSignaturePart2() {
		return signaturePart2;
	}

	public void setSignaturePart2(byte[] signaturePart2) {
		this.signaturePart2 = signaturePart2;
	}

	public SsbNonUic getNonUicData() {
		return nonUicData;
	}

	public void setNonUicData(SsbNonUic nonUicData) {
		this.nonUicData = nonUicData;
		this.nonReservationData = null;
		this.reservationData = null;
		this.groupData = null;
		this.passData = null;		
	}

	public SsbNonReservation getNonReservationData() {
		return nonReservationData;
	}

	public void setNonReservationData(SsbNonReservation nonReservationData) {
		this.nonReservationData = nonReservationData;
		header.setTicketType(SsbTicketType.UIC_2_NRT);
		this.reservationData = null;
		this.nonUicData = null;
		this.groupData = null;
		this.passData = null;		
	}

	public SsbReservation getReservationData() {
		return reservationData;
	}

	public void setReservationData(SsbReservation reservationData) {
		header.setTicketType(SsbTicketType.UIC_1_IRT_RES_BOA);
		this.nonReservationData = null;
		this.nonUicData = null;
		this.groupData = null;
		this.passData = null;
		this.reservationData = reservationData;
	}

	public SsbGroup getGroupData() {
		return groupData;
	}

	public void setGroupData(SsbGroup groupData) {
		this.groupData = groupData;
		header.setTicketType(SsbTicketType.UIC_3_GRP);
		this.nonReservationData = null;
		this.nonUicData = null;
		this.reservationData = null;
		this.passData = null;
		
	}

	public SsbPass getPassData() {
		return passData;
	}

	public void setPassData(SsbPass passData) {
		this.passData = passData;
		header.setTicketType(SsbTicketType.UIC_4_RPT);
		this.nonReservationData = null;
		this.nonUicData = null;
		this.groupData = null;
		this.reservationData = null;		
	}

	public void signLevel1(PrivateKey key, Provider prov, String keyId, String algorithmOid) throws Exception {
		
		
		this.header.setKeyId(Integer.parseInt(keyId));
		
		byte[] data = getDataForSignature();
		
		if (prov == null) {
			//check for a provider supporting the key
			prov = SecurityUtils.findPrivateKeyProvider(key);
		}
			
		//find the algorithm name for the signature OID
		String algo = AlgorithmNameResolver.getSignatureAlgorithmName(algorithmOid, prov);
		Signature sig = null;

		if (prov != null) {
			sig = Signature.getInstance(algo, prov);
		} else {
			sig = Signature.getInstance(algo);
		}
		sig.initSign(key);
		
		sig.update(data);
		
		byte[] signature = sig.sign();
		
		BigInteger[] bInts = SecurityUtils.decodeSignatureIntegerSequence(signature);
		
		signaturePart1 = toUnsignedBytes(bInts[0]);
		
		signaturePart2 = toUnsignedBytes(bInts[1]);
		
		
	}
	
	private static byte[] toUnsignedBytes(BigInteger i) {
		byte[] b = i.abs().toByteArray();
		//remove top sign bit  
		if (b[0] == 0) {
			b = Arrays.copyOfRange(b, 1, b.length);
		}
		return b;
	}

	/**
	 * Verify the signature
	 * 
	 * Note:  an appropriate security provider (e.g. BC) must be registered before 
	 *
	 * @param key the key
	 * @param singningAlg the Object ID of the signing algorithm
	 * @param a dedicated security provider to validate the signature
	 * @return true, if successful
	 * @throws InvalidKeyException the invalid key exception
	 * @throws NoSuchAlgorithmException the no such algorithm exception
	 * @throws SignatureException the signature exception
	 * @throws IllegalArgumentException the illegal argument exception
	 * @throws UnsupportedOperationException the unsupported operating exception
	 * @throws EncodingFormatException 
	 * @throws IOException 
	 */
	public boolean verifyByAlgorithmOid(PublicKey key, String signingAlg, Provider prov) throws InvalidKeyException, NoSuchAlgorithmException, SignatureException, IllegalArgumentException, UnsupportedOperationException, IOException, EncodingFormatException {
		//find the algorithm name for the signature OID
		String algo = null;
		
		
		BigInteger r = new BigInteger(1,signaturePart1);
		BigInteger s = new BigInteger(1,signaturePart2);
		byte[] signature = SecurityUtils.encodeSignatureIntegerSequence(r,s);
		
		String signatureAlgorithmOid = signingAlg;
		
		// guess the signature algorithm based on the signature size
		if ((signingAlg == null || signingAlg.length() < 1) && signature != null) {			
			signatureAlgorithmOid = SecurityUtils.getDsaAlgorithm(signature);
		}	
		
		if (prov != null) {
			Service service = prov.getService("Signature",signatureAlgorithmOid);
			if (service != null) {
				algo = service.getAlgorithm();
			}
		} else {
			Provider[] provs = Security.getProviders();
			for (Provider p : provs) {
			   if (algo == null) {
				   Service service = p.getService("Signature",signatureAlgorithmOid);
				   if (service != null) {
					   algo = service.getAlgorithm();
				   }
			   }
			}

		}
		
		if (algo == null) {
			throw new NoSuchAlgorithmException("No service for algorithm found: " + signingAlg);
		}
		Signature sig = Signature.getInstance(algo);
		sig.initVerify(key);
		sig.update(getDataForSignature());
		
		return sig.verify(signature);
	}


}